

package org.cscarioni

class BacklogController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ backlogInstanceList: Backlog.list( params ), backlogInstanceTotal: Backlog.count() ]
    }

    def show = {
        def backlogInstance = Backlog.get( params.id )

        if(!backlogInstance) {
            flash.message = "Backlog not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ backlogInstance : backlogInstance ] }
    }

    def delete = {
        def backlogInstance = Backlog.get( params.id )
        if(backlogInstance) {
            try {
                backlogInstance.delete(flush:true)
                flash.message = "Backlog ${params.id} deleted"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "Backlog ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "Backlog not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def backlogInstance = Backlog.get( params.id )

        if(!backlogInstance) {
            flash.message = "Backlog not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ backlogInstance : backlogInstance ]
        }
    }

    def update = {
        def backlogInstance = Backlog.get( params.id )
        if(backlogInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(backlogInstance.version > version) {
                    
                    backlogInstance.errors.rejectValue("version", "backlog.optimistic.locking.failure", "Another user has updated this Backlog while you were editing.")
                    render(view:'edit',model:[backlogInstance:backlogInstance])
                    return
                }
            }
            backlogInstance.properties = params
            if(!backlogInstance.hasErrors() && backlogInstance.save()) {
                flash.message = "Backlog ${params.id} updated"
                redirect(action:show,id:backlogInstance.id)
            }
            else {
                render(view:'edit',model:[backlogInstance:backlogInstance])
            }
        }
        else {
            flash.message = "Backlog not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def create = {
        def backlogInstance = new Backlog()
        backlogInstance.properties = params
        return ['backlogInstance':backlogInstance]
    }

    def save = {
        def backlogInstance = new Backlog(params)
        if(!backlogInstance.hasErrors() && backlogInstance.save()) {
            flash.message = "Backlog ${backlogInstance.id} created"
            redirect(action:show,id:backlogInstance.id)
        }
        else {
            render(view:'create',model:[backlogInstance:backlogInstance])
        }
    }
	
	def editBacklogForProject ={
		log.debug("El id del proyecto es ${params.id}")
		def backlogs = Backlog.withCriteria{
    		project{
				eq('id',Long.valueOf(params.id))
			}
		}
		redirect(action: 'edit',params:['id':backlogs[0].id])
	}
}
